package org.firebug.spring.boot.jackson.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * 金额转换(万元)：123456789 -> 1,2345.68万元
 * @Author: 张晓东
 */
public class AmountToWan42Serializer extends JsonSerializer<BigDecimal> {
    private static final String PATTERN = ",####.##";
    private static final BigDecimal WAN = new BigDecimal(10000);

    @Override
    public void serialize(BigDecimal amount, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        String value = new DecimalFormat(PATTERN).format(amount.divide(WAN));
        gen.writeString(value + "万元");
    }
}
